import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:t2app/src/Config.dart';
import 'package:t2app/src/common/AppData.dart';
import 'package:t2app/src/common/Input.dart';
import 'package:t2app/src/entity/User.dart';
import 'package:t2app/src/util/http_request.dart';
import 'package:t2app/src/util/log_util.dart';

import '../../common/Tool.dart';

class LoginView extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new LoginViewState();
  }
}

class LoginViewState extends State<LoginView> {
  //手机号的控制器
  TextEditingController _accountController = TextEditingController();

  //密码的控制器
  TextEditingController _lockController = TextEditingController();
  bool _lockInvisible = true; //密码不可见

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("登录|注册"),
        foregroundColor: Config.THEME.primaryColorDark,
      ),
      body: new SingleChildScrollView(
        child: new Container(
          child: new Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              new Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  new Container(
                    margin: EdgeInsets.fromLTRB(0, 10, 0, 60),
                    child: new Image(
                      image: new AssetImage("assets/images/logo.png"),
                      width: 150,
                      height: 150,
                    ),
                  ),
                ],
              ),
              new Row(
                children: [
                  new Column(
                    children: [
                      new Container(
                        width: MediaQuery.of(context).size.width,
                        child: new FractionallySizedBox(
                          widthFactor: 0.9,
                          child: new Container(
                            padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                            decoration: Input.instance.getBoxDecoration(),
                            child: new TextField(
                              controller: _accountController,
                              decoration: InputDecoration(
                                border: InputBorder.none,
                                icon: Icon(Icons.account_circle),
                                hintText: '请输入你的用户名',
                              ),
                            ),
                          ),
                        ),
                      ),
                      new Container(
                        width: MediaQuery.of(context).size.width * 0.9,
                        decoration: Input.instance.getBoxDecoration(),
                        margin: EdgeInsets.only(top: 20),
                        child: new Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            new Container(
                              width: MediaQuery.of(context).size.width * 0.8,
                              padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                              child: new TextField(
                                controller: _lockController,
                                decoration: InputDecoration(
                                  border: InputBorder.none,
                                  icon: Icon(Icons.lock),
                                  hintText: '请输入你的密码',
                                ),
                                obscureText: _lockInvisible,
                              ),
                            ),
                            new Container(
                              width: MediaQuery.of(context).size.width * 0.1,
                              child: GestureDetector(
                                child: new Icon(
                                  Icons.remove_red_eye,
                                  color: Config.THEME.primaryColor,
                                ),
                                onTapDown: (_) {
                                  setState(() {
                                    _lockInvisible = false;
                                  });
                                },
                                onTapUp: (_) {
                                  setState(() {
                                    _lockInvisible = true;
                                  });
                                },
                              ),
                            ),
                          ],
                        ),
                      ),
                      new Container(
                        margin: EdgeInsets.only(top: 60),
                        width: MediaQuery.of(context).size.width * 0.9,
                        child: new Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            SizedBox(
                              width: 120,
                              // height: 60,
                              child: ElevatedButton(
                                child: new Text("注册"),
                                style: ElevatedButton.styleFrom(
                                  foregroundColor: Config.THEME.primaryColor,
                                  backgroundColor: Colors.white,
                                  iconSize: 30,
                                ),
                                onPressed: () {
                                  EasyLoading.show(status: "注册中");
                                  HttpRequest().request(
                                    Config.DOMAIN + "/user/register",
                                    method: HttpRequestMethod.POST,
                                    data: {
                                      "username": _accountController.text,
                                      "password": _lockController.text,
                                    },
                                    success: (result) {
                                      // LogUtil.p(result);
                                      //缓存
                                      User user = User.fromMap(result["data"]);
                                      AppData.user = user;
                                      user.putStorage();
                                      Tool.EasyLoading_Success("注册成功");
                                      //跳转
                                      Navigator.of(context).pop(true);
                                      Navigator.of(context).pop(true);
                                    },
                                  );
                                },
                              ),
                            ),
                            SizedBox(
                              width: 120,
                              // height: 60,
                              child: ElevatedButton(
                                child: new Text("登录"),
                                style: ElevatedButton.styleFrom(
                                  foregroundColor: Config.THEME.primaryColor,
                                  backgroundColor: Colors.white,
                                ),
                                onPressed: () {
                                  //进行登录注册然后路由去首页
                                  EasyLoading.show(status: "login");
                                  HttpRequest().request(
                                    Config.DOMAIN + "/user/login",
                                    method: HttpRequestMethod.POST,
                                    data: {
                                      "username": _accountController.text,
                                      "password": _lockController.text,
                                    },
                                    success: (result) {
                                      //缓存
                                      User user = User.fromMap(result["data"]);
                                      AppData.user = user;
                                      user.putStorage();
                                      Tool.EasyLoading_Success("登录成功");
                                      Navigator.of(context).pop(true);
                                      Navigator.of(context).pop(true);
                                    },
                                  );
                                },
                              ),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ],
              ),
              new Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  new Container(
                    width: MediaQuery.of(context).size.width * 0.9,
                    margin: EdgeInsets.only(top: 80, bottom: 10),
                    child: new Column(
                      children: [
                        new Container(
                          child: new Text(
                            "<<服务端代码位置>>",
                            style: new TextStyle(
                              color: Config.THEME.primaryColor,
                            ),
                          ),
                        ),
                        new Container(
                          margin: EdgeInsets.only(top: 20),
                          child: new Text(
                            "<<https://gitee.com/tangzhangss/T2Admin-php-tp.git>>",
                            style: new TextStyle(
                              color: Config.THEME.primaryColor,
                            ),
                          ),
                        ),
                        new Container(
                          margin: EdgeInsets.only(top: 20),
                          child: new Text(
                            "@author 抖音（软件万事屋/tangzhangss） 2025 to write",
                            style: new TextStyle(
                              color: Config.THEME.primaryColor,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
